package com.yanqu.road.dataHelper.action;

import com.yanqu.road.dataHelper.action.excel.AddGoodsReource;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.dataHelper.utils.ExcelData;
import com.yanqu.road.entity.servercenter.ServerInfo;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ReadActivity extends actionBase {
    public void go(List<Long> serverList) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        List<Future> futureList = new ArrayList<>();
        String dirPath = System.getProperty("user.dir") + File.separator;
        File file = new File(dirPath + "inExcel");

        String[] list = file.list();


        ExcelData beautyExcel = new ExcelData(dirPath + "inExcel" + File.separator + "Beauty.xlsx", "Sheet1");
        ExcelData skinExcel = new ExcelData(dirPath + "inExcel" + File.separator + "SkinsInfo.xlsx", "Sheet1");
        Map<Integer, String> beautyResourceMap = new HashMap<>();
        for (Map.Entry<String, Map<String, String>> entry : beautyExcel.getExcelData().entrySet()) {
            Map<String, String> value = entry.getValue();
            String beautyId = value.get("id=int");
            String icon = value.get("icon=string");
            String sound = value.get("sound=string");
            String appearanceId = value.get("appearanceId=string");
            beautyResourceMap.put(Integer.valueOf(beautyId), icon + "&" + sound + "&" + appearanceId);
        }
        GameServer.getLogger().error("dataMap Size {}", beautyResourceMap.size());


        Map<Integer, String> skinResourceMap = new HashMap<>();
        for (Map.Entry<String, Map<String, String>> entry : skinExcel.getExcelData().entrySet()) {
            Map<String, String> value = entry.getValue();
            String beautyId = value.get("skinId=int");
            String icon = value.get("icon=string");
            String sound = value.get("sound=string");
            String appearanceId = value.get("appearanceId=string");
            skinResourceMap.put(Integer.valueOf(beautyId), icon + "&" + sound + "&" + appearanceId);
        }
        GameServer.getLogger().error("dataMap Size {}", skinResourceMap.size());


//        activityIdStr = "9888885";
        for (Long serverId : serverList) {
            if (ServerMgr.getServerInfoList().containsKey(serverId)) {
                ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                Future future = executorService.submit(new Callable<Boolean>() {
                    @Override
                    public Boolean call() throws Exception {
                        GameServer testGameServer = new GameServer(serverInfo);
                        AddGoodsReource reource = new AddGoodsReource(testGameServer);
                        reource.go(serverId, beautyResourceMap, skinResourceMap);
                        return true;
                    }
                });
                futureList.add(future);
            }
        }
        isResult(futureList);
//            DataMgr.makeExcelData2Txt("");
//            DataMgr.outText("活动商城购买"+activityId);
        DataMgr.makeExcelData2Txt("");
        DataMgr.outText("活动商城购买");
    }


    public static void main(String[] args) throws IOException {
        String dirPath = System.getProperty("user.dir") + File.separator;
        File file = new File(dirPath + "inExcel");

        String[] list = file.list();
        // 大类型 小类型          条件类型 条件值
        Map<String, Map<String, Map<String, String>>> allMap = new HashMap<>();
        for (String fileName : list) {
            ExcelData main = new ExcelData(dirPath + "inExcel" + File.separator + fileName, "t_s_activity_main");
            int i = 0;
            Map<String, List<String>> activityMap = new HashMap<>();
            Map<String, Map<String,List<String>>> conditionMap = new HashMap<>();
            for (Map.Entry<String, Map<String, String>> entry : main.getExcelData().entrySet()) {
                Map<String, String> value = entry.getValue();
                String acType  = value.get("type=int");
                if (acType == null) {
                    continue;
                }
                if (acType.equals("活动类型")) {
                    continue;
                }
                String signId = value.get("signId=int");
               String childType  = value.get("childType=int");
                List<String> aaa = new ArrayList<>();
                aaa.add(acType);
                aaa.add(childType);
                activityMap.put(signId, aaa);
            }

            ExcelData condition = new ExcelData(dirPath + "inExcel" + File.separator + fileName, "t_s_activity_condition");
            for (Map.Entry<String, Map<String, String>> entry : condition.getExcelData().entrySet()) {
                Map<String, String> value = entry.getValue();
                String conditionType = value.get("type=int");
                if (conditionType == null) {
                    continue;
                }
                if (conditionType.equals("活动条件类型")) {
                    continue;
                }
                String signId = value.get("signId=int");
                String param = value.get("params=string");
                List<String> aaa = new ArrayList<>();
                aaa.add(conditionType);
                aaa.add(param);
                if (!conditionMap.containsKey(signId)) {
                    conditionMap.put(signId, new HashMap<>());
                }
                Map<String, List<String>> listMap = conditionMap.get(signId);
                listMap.put(conditionType, aaa);
            }
            for (Map.Entry<String, List<String>> entry : activityMap.entrySet()) {
                String signId = entry.getKey();
                String mainType = entry.getValue().get(0);
                String childType = entry.getValue().get(1);
                Map<String, List<String>> listMap = conditionMap.get(signId);
                if (listMap == null) {
                    continue;
                }
                for (Map.Entry<String, List<String>> listEntry : listMap.entrySet()) {
                    List<String> stringList = listEntry.getValue();
                    String condtionType = stringList.get(0);
                    String param = stringList.get(1);
                    if (!allMap.containsKey(mainType)) {
                        allMap.put(mainType, new HashMap<>());
                    }
                    Map<String, Map<String, String>> mapMap = allMap.get(mainType);
                    if (!mapMap.containsKey(childType)) {
                        mapMap.put(childType, new HashMap<>());
                    }
                    Map<String, String> map = mapMap.get(childType);
                    map.put(condtionType, param);
                }

            }


        }
        File acFile = new File("D:/活动条件类型.txt");
        FileWriter fileWriter = new FileWriter(acFile);
        BufferedWriter writer = new BufferedWriter(fileWriter);
        for (Map.Entry<String, Map<String, Map<String, String>>> entry : allMap.entrySet()) {
            String atype = entry.getKey();
            for (Map.Entry<String, Map<String, String>> mapEntry : entry.getValue().entrySet()) {
                String childType = mapEntry.getKey();
                for (Map.Entry<String, String> stringEntry : mapEntry.getValue().entrySet()) {
                    String key = stringEntry.getKey();
                    String value = stringEntry.getValue();
                    System.out.println(atype + "      " + childType + "     " + key + "         " + value + "\n");
                    writer.write(atype + " " + childType + " " + key + " " + value + "\n");
                }
            }
        }
        writer.flush();
    }
}
